package com.android.lovingpet.bean;

import android.os.Parcel;
import android.os.Parcelable;
import android.support.annotation.NonNull;
import android.util.Log;
import com.android.lovingpet.util.Utility;
import org.litepal.crud.DataSupport;
import java.util.ArrayList;
import java.util.List;

public class Account extends DataSupport implements Parcelable {

    private String head;//存放的是头像的链接
    private int id;
    private ArrayList<Integer> fans=new ArrayList<>();
    private ArrayList<Integer> likes=new ArrayList<>();
    private int realId;
    private int grade;
    private int loveValue;
    private String accept;
    private  String name;
    private int gender;
    private int age;
    private String phone;
    private String email;
    private String synopsis;
    private String city;
    private String job;

    public ArrayList<Integer> getFans() {
        if (fans==null)
        {
            return new ArrayList<>();
        }
        return fans;
    }

    public void setFans(ArrayList<Integer> fans) {
        this.fans = fans;
    }

    public ArrayList<Integer> getLikes() {
        if (likes==null)
        {
            return new ArrayList<>();
        }
        return likes;
    }

    public void setLikes(ArrayList<Integer> likes) {
        this.likes = likes;
    }
    public int getRealId() {
        return realId;
    }

    public void setRealId(int realId) {
        this.realId = realId;
    }

    public String getAccept() {
        return accept;
    }

    public void setAccept(String accept) {
        this.accept = accept;
    }



    public Account() {
        this.likes=new ArrayList<>();
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getHead() {
        return head;
    }

    public void setHead(String head) {
        this.head = head;
    }



    public int getGrade() {
        return grade;
    }

    public void setGrade(int grade) {
        this.grade = grade;
    }

    public int getLoveValue() {
        return loveValue;
    }

    public void setLoveValue(int loveValue) {
        this.loveValue = loveValue;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getGender() {
        return gender;
    }

    public void setGender(int gender) {
        this.gender = gender;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getSynopsis() {
        return synopsis;
    }

    public void setSynopsis(String synopsis) {
        this.synopsis = synopsis;
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public String getJob() {
        return job;
    }

    public void setJob(String job) {
        this.job = job;
    }

    public synchronized boolean saveOrUpdate()
    {
        return saveOrUpdate("realId=?",realId+"");
    }
    public synchronized boolean saveOrUpdate(String... conditions)
    {
        //存在就更新数据
        if (Utility.isSaved(conditions)==true)
        {
            Log.d("save account","存在");
            updateAll(conditions);
        }
        else
        {
            Log.d("save account","不存在");

            if (save())
            {
                return true;
            }
            else
                return false;
        }
        return true;
    }
    public synchronized static List<Account> findByRealid(@NonNull ArrayList<Integer> ids )
    {
        //如果传入的是null
        if (ids==null)
        {
            Log.d("ids","null");
            return new ArrayList<>();
        }
        if (ids.isEmpty())
        {
            Log.d("ids","null");
            return new ArrayList<>();
        }
        long[] idArray=new long[ids.size()];
        int i=0;
        for (int id : ids) {
            Log.d("ids",id+"");
             idArray[i++]=id;
        }

       // List<Account> accounts = DataSupport.findAll(Account.class,idArray);
        List<Account> accounts = Utility.getAccounts(idArray);
        Log.d("ids",accounts.size()+"");
        return accounts;
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(this.head);
        dest.writeInt(this.id);
        dest.writeList(this.fans);
        dest.writeList(this.likes);
        dest.writeInt(this.realId);
        dest.writeInt(this.grade);
        dest.writeInt(this.loveValue);
        dest.writeString(this.accept);
        dest.writeString(this.name);
        dest.writeInt(this.gender);
        dest.writeInt(this.age);
        dest.writeString(this.phone);
        dest.writeString(this.email);
        dest.writeString(this.synopsis);
        dest.writeString(this.city);
        dest.writeString(this.job);
    }

    protected Account(Parcel in) {
        this.head = in.readString();
        this.id = in.readInt();
        this.fans = new ArrayList<Integer>();
        in.readList(this.fans, Integer.class.getClassLoader());
        this.likes = new ArrayList<Integer>();
        in.readList(this.likes, Integer.class.getClassLoader());
        this.realId = in.readInt();
        this.grade = in.readInt();
        this.loveValue = in.readInt();
        this.accept = in.readString();
        this.name = in.readString();
        this.gender = in.readInt();
        this.age = in.readInt();
        this.phone = in.readString();
        this.email = in.readString();
        this.synopsis = in.readString();
        this.city = in.readString();
        this.job = in.readString();
    }

    public static final Creator<Account> CREATOR = new Creator<Account>() {
        @Override
        public Account createFromParcel(Parcel source) {
            return new Account(source);
        }

        @Override
        public Account[] newArray(int size) {
            return new Account[size];
        }
    };
}